package com.yashi.springmvcjbpm.controller;


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jbpm.api.model.ActivityCoordinates;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.yashi.springmvcjbpm.util.CompleteTaskHistory;
import com.yashi.springmvcjbpm.util.ImageShow;

@Controller
public class ShowImageController {
	ImageShow imageShow = new ImageShow();
	@RequestMapping("/showImage")
	public String show(HttpServletRequest request,String processInstanceId) {
		
		// 流程图活动坐标
		List<CompleteTaskHistory> activityCoordinates = imageShow.findActivityCoordinatesList(processInstanceId);
		List<CompleteTaskHistory> ac = imageShow.findHistoryActivityInfo(processInstanceId);
	
		request.setAttribute("ac", ac);
		request.setAttribute("activityCoordinates", activityCoordinates);
		request.setAttribute("pid", processInstanceId);
		return "imageShow";
	}
	@RequestMapping("/processImage")
	public void pic(HttpServletRequest request,HttpServletResponse response) {		
		ImageShow imageShow = new ImageShow();
		String processInstanceId = request.getParameter("pid");
		InputStream inputStream = imageShow.findProcessInstancePic(processInstanceId);	
		PrintWriter pw = null;
		if (inputStream == null) {
			try {
				pw = response.getWriter();
				pw.write("error");
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				pw.close();
			}
		} else {
			byte[] b = new byte[1024];
			int len = -1;
			ServletOutputStream sos = null;
			try {
				sos = response.getOutputStream();
				while ((len = inputStream.read(b, 0, 1024)) != -1) {
					sos.write(b, 0, len);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (sos != null) {
					try {
						sos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
